﻿package CutLoad.components
{
	import CutLoad.*;
	import CutLoad.cookies.Fun;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	/**
	 * 下拉列表 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CComboBox extends Base
	{
		//是否设置皮肤
		private var isSetKin:Boolean=false;
		//提示
		private var strTip:String;
		//下拉按钮左边那个箭头的宽度
		private var btnCombo_leftWidth:Number
		//下拉按钮
		private var btnCombo:CButton;
		//
		private var arrData:Array;
		//最宽
		private var maxWidth:Number=0
		//是否显示或隐藏下拉按钮组
		private var isOpen:Boolean=false
		//
		private var funClickItem:Function
		private var spBtnsParent:Sprite;
		//显示的种类
		private var downType:uint		
		//显示的区域
		private var mouseOpenArea:Rectangle;
		//显示区域Sprite
		private var spViewArea:Sprite=null
		//所有下拉按钮都放在这里，方便控制深度
		private var spBtns:Sprite;
		//当前选重的标签
		private var currentObj:Object=null;
		//
		/**
		 * 返回一个默认的CComboBox实例 
		 * @param parent
		 * @param downType
		 * @param tip
		 * @param clickItem
		 * @return 
		 * 
		 */		
		public static function getSimple(parent:Sprite,downType:uint=0,tip:String="请选择..",clickItem:Function=null):CComboBox{
			var cb:CComboBox=new CComboBox();
			cb.init(parent,downType,tip,clickItem);
			return cb;
		}
		public function CComboBox()
		{
		}
		/**
		 * 初始化		
		 * @param parent  弹出的下拉列表显示的父类,一般指向顶级显示对象，这样下拉列表才不会被盖住。
		 * @param downType 下拉列表显示的种类:0 向下  1向上  2水平排列向下  3水平排列向上
		 * @param tip 没有选择时的提示
		 * @param clickItem 点击列表项时要处理的方法 function(obj:Object)  obj的格式：{label:label,data:data}
		 * 
		 */		
		public function init(parent:Sprite,downType:uint=0,tip:String="请选择..",clickItem:Function=null):void{
			if(downType<0||downType>3) throw new Error("下拉列表显示的种类只能是:0 向下  1向上  2水平排列向下  3水平排列向上");
			strTip=tip;
			spBtnsParent=parent;
			spBtns=new Sprite();
			//
			spBtnsParent.addChild(spBtns);
			this.downType=downType;
			mouseOpenArea=new Rectangle();
			//
			this.funClickItem=clickItem
			arrData=new Array();
			//
			btnCombo_leftWidth=16
			if(!isSetKin){
				var sysKinTem:Array=Skin.getInstance().getSkin(3);
				btnCombo_leftWidth=sysKinTem[1][0]
				setSkin(sysKinTem)
			}	
		}
		/**
		 * 设置皮肤
		 * @param arrClasname
		 * @return 
		 * 
		 */		
		public function setSkin(arrClasname:Array):CComboBox{
			btnCombo=new CButton();
			btnCombo.setSkin(arrClasname[0],strTip);
			btnCombo.init(strTip,true,true,false);
			btnCombo.addEventListener(MouseEvent.CLICK,btnCombo_click);
			this.addChild(btnCombo);
			//
			isSetKin=true;
			return this;
		}
		/**
		 * 设置 点击列表项时要处理的方法 
		 * @param clickItem  格式： function(obj:Object)  obj的格式：{label:label,data:data}
		 * @return 
		 * 
		 */	
		public function setFunClickItem(clickItem:Function):void{
			this.funClickItem=clickItem;
		}
		private function btnCombo_click(e:MouseEvent):void{
			if(!this.isOpen){
				this.open()
			}else{
				this.close();
			}
		}
		private function addCheckMouseOnMe():void{
			if(!this.hasEventListener(Event.ENTER_FRAME)){
				this.addEventListener(Event.ENTER_FRAME,CheckMouseOnMe_fun);
				//trace("addd......")
			}
		}
		//检测鼠标坐标，来判断是否要收回下拉列表框。
		private function CheckMouseOnMe_fun(e:Event):void{
			//
			var mousexx:Number=this.spBtnsParent.mouseX;
			var mouseyy:Number=this.spBtnsParent.mouseY;
			var x1:Number=mouseOpenArea.x;
			var x2:Number=mouseOpenArea.x+mouseOpenArea.width;
			var y1:Number=mouseOpenArea.y;
			var y2:Number=mouseOpenArea.y+mouseOpenArea.height;
			if(mousexx>=x1&&mousexx<=x2&&mouseyy>=y1&&mouseyy<=y2){
				if(this.downType==2||this.downType==3){
					x1=this.x+this.btnCombo.getShowWidth();
					y1=this.y
					y2=this.y+this.btnCombo.getShowHeight();
					//
					/*var ggg:Graphics=spBtnsParent.graphics;
					ggg.clear()
					ggg.beginFill(0x0000ff,0.3);
					ggg.drawRect(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1));
					ggg.endFill()*/
					if(mousexx>=x1&&mousexx<=x2&&mouseyy>=y1&&mouseyy<=y2){
						this.close();
					}
				}
			}else{
				this.close();
			}
		}
		private function removeCheckMouseOnMe():void{
			if(this.hasEventListener(Event.ENTER_FRAME)){
				//trace("close..")
				this.removeEventListener(Event.ENTER_FRAME,CheckMouseOnMe_fun);
			}
			
		}
		/**
		 * 返回当前选重的对象 
		 * @return object对象，格式：{label:label,data:data}
		 * 
		 */		
		public function get value():Object{
			return currentObj;
		}
		/**
		 * 返回数据对象,一个数组，每一个元素的数据结构为： {label:文本内容,data:数据信息,btn:对应的按钮}
		 * @return 
		 * 
		 */		
		public function getData():Array{
			return this.arrData;
		}
		/**
		 * 添加一条记录 注：添加的对象应该有属性：{label:文本内容,data:数据信息} 至少有label这个属性
		 * @param label 标签
		 * @param data 数值
		 * @return 
		 * 
		 */		
		public function addItem(label:String,data:String=null):CComboBox{
			var item:Object={label:label,data:data};
			if(item.label==null) throw new Error("添加的对象应该有属性：{label:文本内容,data:数据信息} 至少有label这个属性");
			//如果没有data属性，系统自加一个
			if(item.data==null) item.data=item.label;
			//
			var itemBtn:CButton=new CButton();
			itemBtn.init(label,true,true,false,true);
			itemBtn.setOtherInfo(item);
			item.btn=itemBtn;
			//父显示对象的坐标
			setMaxWidth(itemBtn)
			//
			var parentR:Rectangle=this.getRect(spBtnsParent);
			//
			switch(downType){
				case 1:
					itemBtn.x=parentR.x;
					if(this.arrData.length!=0){
						itemBtn.y=this.arrData[this.arrData.length-1].btn.y-itemBtn.getShowHeight();
					}else{
						itemBtn.y=btnCombo.y+parentR.y-itemBtn.getShowHeight();
					}
					
					mouseOpenArea.x=itemBtn.x;
					mouseOpenArea.y=itemBtn.y;
					mouseOpenArea.width=maxWidth;
					mouseOpenArea.height=btnCombo.getShowHeight()+Math.abs(itemBtn.y-parentR.y);
					break;
				case 2:
				case 3:
					if(downType==2){
						itemBtn.y=btnCombo.y+itemBtn.getShowHeight()+parentR.y
					}else{
						itemBtn.y=btnCombo.y-itemBtn.getShowHeight()+parentR.y
					}
					if(this.arrData.length!=0){
						itemBtn.x=this.arrData[this.arrData.length-1].btn.getXYRightUp().x;
					}else{
						itemBtn.x=parentR.x;
					}
					
					if(downType==2){
						mouseOpenArea.x=parentR.x;
						mouseOpenArea.y=parentR.y;
					}else{
						if(this.arrData.length==0){
							mouseOpenArea.x=itemBtn.x;
							mouseOpenArea.y=itemBtn.y;
						}else{
							mouseOpenArea.x=this.arrData[0].btn.x;
							mouseOpenArea.y=this.arrData[0].btn.y;
						}
					}
					mouseOpenArea.width=itemBtn.getXYRightUp().x-parentR.x;
					mouseOpenArea.height=itemBtn.getShowHeight()+btnCombo.getShowHeight();
					break;
				default:
					itemBtn.x=parentR.x;
					if(this.arrData.length!=0){
						itemBtn.y=this.arrData[this.arrData.length-1].btn.getXYRightDown().y;
					}else{
						itemBtn.y=btnCombo.getXYRightDown().y+parentR.y;
					}
					mouseOpenArea.x=parentR.x;
					mouseOpenArea.y=parentR.y;
					mouseOpenArea.width=maxWidth;
					mouseOpenArea.height=itemBtn.getXYRightDown().y-parentR.y
					break;
			}
			//
			itemBtn.addEventListener(MouseEvent.CLICK,itemBtn_ClickItem);
			//			
			itemBtn.show(spBtns);
			//itemBtn.printInfon();
			itemBtn.visible=this.isOpen;			
			//
			this.arrData.push(item);
			//showGuide()
			return this
		}
		/**
		 * 显示附助边框
		 * 
		 */		
		public function showGuide():void{
			if(spViewArea!=null) spBtnsParent.removeChild(spViewArea);
			spViewArea=new Sprite();
			spViewArea.buttonMode=false;
			this.spBtnsParent.addChild(spViewArea);
			var ggg:Graphics=spViewArea.graphics;
			ggg.lineStyle(1,0xff0000,1);
			ggg.beginFill(0xff0000,0);
			ggg.drawRect(mouseOpenArea.x,mouseOpenArea.y,mouseOpenArea.width,mouseOpenArea.height);
			ggg.endFill()
		}
		private function itemBtn_ClickItem(e:MouseEvent):void{
			var btn:CButton=e.target as CButton;
			this.btnCombo.label=btn.label;
			//
			var objTem:Object=btn.getOtherInfo();
			currentObj={label:objTem.label,data:objTem.data};
			if(this.funClickItem!=null) funClickItem(btn.getOtherInfo());
			
		}
		/**
		 * 一次添加许多的项，请输入一个对象数组，每一个项的为： {label:文本内容,data:数据信息},至少有label这个属性")
		 * @param arr 对象数组
		 * @return
		 * 
		 */		
		public function addSomeItem(arr:Array):CComboBox{
			for(var i:Number=0;i<arr.length;i++){
				addItem(arr[i].label,arr[i].data);
			}
			return this;
		}
		/**
		 * 显示下拉列表
		 * @return 
		 * 
		 */		
		public function open():CComboBox{
			if(!this.isOpen){
					var arrClone:Array=new Array();
					for(var i:Number=0;i<arrData.length;i++){
						var item:Object=arrData[i];
						arrClone.push({label:item.label,data:item.data});
					}
					spBtnsParent.removeChild(spBtns);
					init(spBtnsParent,downType,strTip,funClickItem);
					this.addSomeItem(arrClone);
				SH(true)
				this.addCheckMouseOnMe();
				//Fun.setTopDepath(spBtns);
			}
			return this;
		}
		/**
		 * 关闭下拉 列表
		 * @return 
		 * 
		 */		
		public function close():CComboBox{
			if(this.isOpen) {
				SH(false)
				this.removeCheckMouseOnMe()
			}
			return this;
		}
		//显示或隐藏下拉的按钮组
		private function SH(isOk:Boolean):void{
			//
			isOpen=isOk;
			//深度放在最上边
			if(isOpen){
				var spTop:Sprite=new Sprite();
				spBtnsParent.addChild(spTop);
				spBtnsParent.swapChildren(spTop,spBtns);
				spBtnsParent.removeChild(spTop);
			}
			for(var i:Number=0;i<arrData.length;i++){
				var btnTem:CButton=arrData[i].btn;
				btnTem.visible=isOk
			}
			//
		}
		//设置最高值
		private function setMaxWidth(btn:CButton):void{
			if(maxWidth<btn.getShowWidth()){
				maxWidth=btn.getShowWidth();
				//加上箭头那个宽度
				maxWidth+=btnCombo_leftWidth
				btnCombo.AutoResizeWidth=false;
				btnCombo.setWH(maxWidth,btnCombo.getShowHeight());
				//
				for(var i:Number=0;i<arrData.length;i++){
					var btnTem:CButton=arrData[i].btn;
					btnTem.AutoResizeWidth=false;
					btnTem.setWH(maxWidth,btnTem.getShowHeight());
				}
				btn.AutoResizeWidth=false;
				btn.setWH(maxWidth,btn.getShowHeight());
				
			}else if(maxWidth>btn.getShowWidth()){
				btn.AutoResizeWidth=false;
				btn.setWH(maxWidth,btn.getShowHeight());
			}
		}
		/**
		 * 此方法无效 
		 * @param w
		 * @param h
		 * 
		 */		
		override public function setWH(w:Number=0, h:Number=0):void{
			
		}
	}
}